#coding=utf-8
import time
import json
import shutil
import urllib.parse,urllib.request,http.cookiejar,os,re 
# 获取外网IP
def get_out_ip2():
    url = r'http://1212.ip138.com/ic.asp'
    #url = r'http://city.ip138.com/ip2city.asp'
    with urllib.request.urlopen(url) as f:
        
        data = f.read()

        #print('Status:', f.status, f.reason)
        #for k, v in f.getheaders():
            #print('%s: %s' % (k, v))
        txt = data.decode("GBK")
        
    ip = txt[txt.find("[") + 1: txt.find("]")]
    print('ip:' + ip)
    return ip
def get_out_ip():
    #http://blog.163.com/rz_xiaojia/blog/static/119209198201641232913864/
    url = r'http://ip.chinaz.com/getip.aspx'
    with urllib.request.urlopen(url) as f:
        
        data = f.read()
        #print(data)
        #print('Status:', f.status, f.reason)
        #for k, v in f.getheaders():
            #print('%s: %s' % (k, v))
        txt = data.decode("UTF8")
        
    ip = re.search('\d+\.\d+\.\d+\.\d+',txt).group(0)
    print('ip:' + ip)
    return ip
#访问链接发送数据
def getUrlRequest(iUrl,iStrPostData): 
    postdata=urllib.parse.urlencode(iStrPostData) 
    postdata=postdata.encode(encoding='UTF8') 
    header = {'User-Agent':'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)'} 
    req= urllib.request.Request( 
               url = iUrl, 
               data = postdata, 
               headers = header) 
    return urllib.request.urlopen(req).read().decode("UTF8")
#记录
def log(data,file):
    logTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    with open(file, 'a') as f:
        data = '\n '+logTime +' '+data
        f.write(data)
##清除火狐缓存
def clearFfCache():
    list = [
        'C:/Users/Administrator/AppData/Local/Mozilla/Firefox/Profiles/',
        'C:/Users/Administrator/AppData/Local/Mozilla/Firefox/updates/'
            ]
    try:
        for i in range(len(list)):
            if(os.path.exists(list[i])):
                shutil.rmtree(list[i])
            #os.mkdir(list[i])
    except:
        print('【!】清除火狐缓存失败，可能因为开启了多个火狐浏览器');
    
if __name__ == "__main__":
    clearFfCache()
